import Vue from 'vue'
import Vuex from 'vuex'

import getters from './getters'

//安装插件
Vue.use(Vuex)

//创建Store对象
const store = new Vuex.Store({
    state:{
        cartList:[] ,              //创建cartList数组，存放添加的内容
    },
    getters,
    mutations:{
        addCart(state,payload){
            //判断添加的商品payload是否存在
            console.log('add to cart method:...........',payload)
            let oldProduct  = null
            for(let item of state.cartList){
                if(item.iid == payload.iid){
                    oldProduct =item
                }
            }

            //判断oldProduct
            if(oldProduct){
                oldProduct.count+=1
            }else{
                payload.count = 1
                payload.checked = true
                state.cartList.push(payload)
            }
        }
    }
})

//挂载Vue实例
export default store
